﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetTaste;
using SixLabors.ImageSharp.Drawing;

namespace Tgather
{
    public partial class PointListForm : Form
    {
        Config config = new Config();

        public string p_name = "";

        private string p_id = "";
        /// <summary> mysql
        /// 
        /// </summary>
        SqlSugarHelper mySQL = new SqlSugarHelper();

        /// <summary>分页每页多少条
        /// 
        /// </summary>
        int pageSize = 5;

        /// <summary>分页当前页码
        /// 
        /// </summary>
        int pagenumber = 1;

        /// <summary>数据总量
        /// 
        /// </summary>
        int totalCount = 0;

        public PointListForm()
        {
            this.StartPosition = FormStartPosition.Manual;
            InitializeComponent();
            Screen primaryScreen = Screen.PrimaryScreen!;
            this.MaximizedBounds = primaryScreen.WorkingArea;
            this.MinimumSize = primaryScreen.WorkingArea.Size;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

            this.pointDataGridView.Top = this.groupBox1.Top;
            this.pointDataGridView.Left = this.groupBox1.Left + this.groupBox1.Width + 10;
            this.pointDataGridView.Width = this.Width - this.groupBox1.Left - this.groupBox1.Width - 40;
            this.pointDataGridView.Height = this.Height - this.groupBox1.Top - 90;

            this.pointPagination.TotalCount = 0;
            this.pointPagination.PageSize = pageSize;
        }

        /// <summary> 创建DataGridViewTextBoxCell
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private DataGridViewTextBoxCell dataGridViewBoxCell(string str)
        {
            DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
            cell1.Value = str;
            return cell1;
        }

        /// <summary>窗口禁止调整大小
        /// 
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (m.Msg == 0x112)
            {
                switch ((int)m.WParam)
                {
                    //禁止双击标题栏关闭窗体
                    case 0xF063:
                    case 0xF093:
                        m.WParam = IntPtr.Zero;
                        break;

                    //禁止拖拽标题栏还原窗体
                    case 0xF012:
                    case 0xF010:
                        m.WParam = IntPtr.Zero;
                        break;

                    //禁止双击标题栏
                    case 0xf122:
                        m.WParam = IntPtr.Zero;
                        break;

                    //允许关闭按钮
                    case 0xF060:
                        //m.WParam = IntPtr.Zero;
                        break;

                    //允许最大化按钮
                    case 0xf020:
                        //m.WParam = IntPtr.Zero;
                        break;

                    //允许最小化按钮
                    case 0xf030:
                        //m.WParam = IntPtr.Zero;
                        break;

                    //禁用还原按钮
                    case 0xf120:
                        //m.WParam = IntPtr.Zero;
                        break;

                    default:
                        //m.WParam = IntPtr.Zero;
                        break;
                }
            }
            base.WndProc(ref m);
        }

        private void PointList_Load(object sender, EventArgs e)
        {
            freshProjectInfo();
        }

        /// <summary>刷新任务信息
        /// 
        /// </summary>
        private async void freshProjectInfo()
        {
            IndexForm frm = (IndexForm)this.Owner!;
            HttpClient client = frm.httpClient();
            var url = config.APIAddress + "/Proj/query";
            JObject data = new JObject();
            data["pageNo"] = pagenumber;
            data["pageSize"] = pageSize;
            data["pname"] = p_name;
            string sendData = JsonConvert.SerializeObject(data);
            try
            {
                HttpResponseMessage response = await client.PostAsync(url,
                new StringContent(sendData, Encoding.UTF8, "application/json"));
                var str = await response.Content.ReadAsStringAsync();
                Response responseObj = new Response();
                if (str != null && str.Length > 0)
                {
                    Debug.WriteLine(str);
                    responseObj = JsonConvert.DeserializeObject<Response>(str)!;
                    List<ProjectItem> list = responseObj.list!;
                    if (list != null)
                    {
                        ProjectItem project = list[0];
                        p_id = project.id!;
                        startTimeLabel.Text = project.startTime!;
                        endTimeLabel.Text = project.endTime!;
                        unitLabel.Text = project.unit;
                        constructionTypeLabel.Text = project.constructionType;
                        pNameLabel.Text = project.pname;
                        pChargeLabel.Text = project.pcharge;
                        freshPointList();
                    }
                }

            }
            catch (HttpRequestException ex)
            {
                Debug.WriteLine("查询项目接口请求超时：【" + ex.Message + "】");
            }

        }

        /// <summary>刷新点位列表
        /// 
        /// </summary>
        private void freshPointList()
        {
            if (p_id.Length > 0)
            {
                this.pointDataGridView.Rows.Clear();

                Dictionary<string, Object> where2 = new Dictionary<string, Object>();
                where2["p_id"] = p_id;
                //单表分页
                List<PPoint> list = mySQL.Db.Queryable<PPoint>().Where(mySQL.dictionaryToConditional(where2)).OrderBy((st) => st.create_time, OrderByType.Desc).ToPageList(pagenumber, pageSize, ref totalCount);

                this.pointPagination.TotalCount = totalCount;
                updateDataGridViewRow(list);
            }

        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void updateDataGridViewRow(List<PPoint> list)
        {
            this.pointDataGridView.Rows.Clear();
            foreach (PPoint item in list)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(dataGridViewBoxCell(item.inlet_pressure.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.inlet_temperature.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.inlet_instantaneous_flow.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.inlet_cumulative_flow.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.outlet_pressure.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.outlet_temperature.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.outlet_instantaneous_flow.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.outlet_cumulative_flow.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.box_temperature.ToString()));
                row.Height = 40;
                this.pointDataGridView.Rows.Add(row);
            }
        }

        /// <summary>翻页
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pagingSource"></param>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        private void pointPagination_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            if (count > 0)
            {
                pagenumber = pageIndex;
                freshPointList();
            }
        }
    }
}
